package com.demo.mqtt.daoimpl;

import com.demo.mqtt.dao.UserDao;
import com.demo.mqtt.po.UserPO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

/**
 * Created by fukunaka on 2017/6/4.
 */
@Repository
public class UserDaoImpl extends BaseDao implements UserDao {
    private String TABLENAME = "mq_user";

    private String SQL_FINDUSER = "SELECT * FROM " + TABLENAME + " WHERE username = ? ";

    private String SQL_INSERTUSER = "INSERT INTO " + TABLENAME + " (id,username,password) values (?,?,?)";

    public UserDaoImpl(DataSource dataSource) {
        super(dataSource);
    }

    @Override
    public UserPO finduserbyusername(String username) {
        try {
            return getJdbcTemplate().queryForObject(SQL_FINDUSER, new Object[]{username}, new BeanPropertyRowMapper<>(UserPO.class));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean insertuser(UserPO userPO) {
        try {
            if (getJdbcTemplate().update(SQL_INSERTUSER, userPO.getId(), userPO.getUsername(), userPO.getPassword()) > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
